<?php
namespace app\common\model;
use think\Model;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/6
 * Time: 18:05
 */
class LitestoreShop extends Base {
    protected $pk = 'id';
    public function getShopByComment(){
        return $this->hasMany("LitestoreShopComment","sid","id")->where('status',0)->field('id,comment,createtime,score,sid,uid,createtime')->with("getCommentByUser");
    }
    public function getShopByTag(){
        return $this->belongsToMany("LitestoreTag","LitestoreTagMid","tid","lid");
    }
    public function getShopByCategory(){
        return $this->hasOne("LitestoreShopcategory","id","category_id")->field("id,name");
    }

    /**
     * 商家推荐
     */
    public function getShopByPosidMid(){
        return $this->belongsToMany("LitestorePosid","LitestorePosidMid","tid","lid");
    }


    /**
     * 通过推荐位id找商家推荐
     */
    public function getShop_By_PosidMid(){
        return $this->belongsToMany("LitestoreShop","LitestorePosidMid","lid","tid");
    }
    /**
     * 商家地址
     * @return \think\model\relation\HasMany
     */
    public function getAddressbyUser(){
        return $this->hasMany("LitestoreOrderAddress","user_id","id");
    }
    /**
     * 通过经纬度查询最近的商店
     */
    public static function nearbyLatShop(){
        $list=[];
        $model = new self();
        $model = $model->alias('a');
//        id,name,shop_image,longitude,latitude,category_id
//             $model = $model->join('LitestoreTagMid m','a.id = m.lid');
//             $model = $model->join('LitestoreTag t','t.id = m.tid');
        $model = $model->field('a.id,a.name,a.shop_image,a.longitude,a.latitude,a.category_id');
        $list = $model->select();
        if($list){
            return collection($list)->toArray();
        }
    }


    /**
     * 商品列表
     */
    public function getList($map,$order,$field,$page){
        $LitestoreShopModel = $this->alias('s')->where($map)->order($order)->field($field)->page($page)->limit(10);
        $datas = collection($LitestoreShopModel->with("getShopByCategory")->with("getShopByTag")->select())->toArray();

        return $datas;
    }

}